using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sbMembership.Library.Presenters.ViewInterfaces;
using sbMembership.Library.Presenters;
using sbMembership.Library.Domain;

namespace sbMembership.Web.Views
{
    public partial class EditRoleView : System.Web.UI.UserControl, IEditRoleView
    {
        #region IEditRoleView Members

        public string Message
        {
            set { lblMessage.Text = value; }
        }

        public string RoleToEdit
        {
            get
            {
                return fldRoleName.Value;
            }
            set
            {
                fldRoleName.Value = value;
            }
        }

        public string[] UsersInRole
        {
            set
            {
                List<sbUser> userList = new List<sbUser>();
                foreach (string user in value)
                {
                    userList.Add(new sbUser(user, null, null));
                }

                gvMembers.DataSource = userList;
                gvMembers.DataBind();
            }
        }

        public MembershipUserCollection AvailableUsers
        {
            set
            {
                lstUsers.DataSource = value;
                lstUsers.DataBind();
            }
        }

        EditRolePresenter presenter;
        public void AttachPresenter(EditRolePresenter presenter)
        {
            this.presenter = presenter;
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gvMembers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int rowID = Int32.Parse(e.CommandArgument.ToString());
                GridViewRow gr = gvMembers.Rows[rowID];
                string userName = gr.Cells[0].Text;
                presenter.RemoveUser(userName);
            }

        }

        protected void gvMembers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnAssignRoles_Click(object sender, EventArgs e)
        {
            presenter.AssignUser(lstUsers.SelectedValue);
        }

    }
}